<?php
//单例模式
//判断只有一个对象  
 //最终代码
class Single{
	protected $rnd;
	protected  static $ins = null;
	public function __construct(){
		$this->rnd = mt_rand(0,100);
	}
	public static function getins(){
		if(self::$ins === null){
			self::$ins = new self();
		}
		return self::$ins;
	}
	private function __clone(){} //禁止克隆
}
$s1 = Single::getIns();
$s2 = Single::getIns();

//$s3 = clone $s2 ; //clone的时候会报错 不能clone 
print_r($s1);
print_r($s2);
var_dump($s2===$s1);
//var_dump($s3===$s2);


/*
//第一次代码
class Single{
	protected $rnd;
	public function __construct(){
		$this->rnd = mt_rand(0,100);
	}
}
//外界可以自由的new 
$a =new Single();
print_r($a);

// 关键 防止别人new
//第二次代码修改
class Single{
	protected $rnd;
	public function __construct(){
		$this->rnd = mt_rand(0,100);
	}
	public static function getins(){
		return new self();
	}
}
$s1 = Single::getIns();
$s2 = Single::getIns();
print_r($s1);
print_r($s2); //依然是两个对象

//判断只有一个对象  
//第三次修改
class Single{
	protected $rnd;
	protected  static $ins = null;
	public function __construct(){
		$this->rnd = mt_rand(0,100);
	}
	public static function getins(){
		if(self::$ins === null){
			self::$ins = new self();
		}
		return self::$ins;
	}
	private function __clone(){} //禁止克隆
}
$s1 = Single::getIns();
$s2 = Single::getIns();
$s3 = clone $s2 ;
print_r($s1);
print_r($s2);
var_dump($s2===$s1);
var_dump($s3===$s2);
*/
?>